<template>
  <!-- 主页容器 -->
    <el-container class="home_container">
      <!-- 头部区域 -->
    <el-header>
      <div>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
      </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-box" @click="togglecollapse">|||</div>
        <el-menu
          background-color="#343a47"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activepath"
          >
          <el-submenu :index="item.id + ''"  v-for="item in asideList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]" class="iconmargin"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item-group v-for="subitem in item.children" :key="subitem.id">
              <el-menu-item :index="'/' + subitem.path" @click="saveactivepath('/' + subitem.path)">
                <i class="el-icon-menu"></i>
                <span slot="title">{{subitem.authName}}</span>
              </el-menu-item>
              
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import {getdata} from '../../network/getdata'
export default {
  name:'home',
  data() {
    return {
      asideList:[],
      //图标对象
      iconObj:{
        '125':'icon-users',
        '101':'icon-store',
        '102':'icon-clipboard',
        '145':'icon-chart-pie',
        '103':'icon-cube'
      },
      //是否折叠 -- false 不开启
      isCollapse:false,
      //活跃的路由
      activepath:''
    }
  },
  methods: {
    //保存点击时的路由
    saveactivepath(activepath) {
      window.sessionStorage.setItem('activepath',activepath)
      this.activepath = activepath
    },
    //切换菜单栏折叠
    togglecollapse() {
      this.isCollapse = !this.isCollapse
    },
    //请求侧边栏数据
    getasidedata() {
      getdata({
        methed:'get',
        url:'/menus'
      }).then(result => {
        // console.log(res.data)
        const res = result.data
        if(res.meta.status !== 200) {
          //请求数据失败
          this.$message.error(res.meta.msg);
          return ;
        }
        //请求数据成功
        this.asideList = res.data
      })
    },
    //退出登录
    logout() {
      //清除 sessionstorage 中 的 token 跳转到登录页面
      window.sessionStorage.clear()
      this.$router.push('/login')
      this.$message.success('退出成功')
    }
  },
  created() {
    this.getasidedata()
    //获取当前活跃的路由
    this.activepath = window.sessionStorage.getItem('activepath')
  },
}
</script>

<style lang="less" scoped>
  .home_container {
    height: 100%;
  }
  //  element ui 中的标签名就是 类名
  .el-header {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    background-color: rgb(50,55,68);
  }
  .el-aside {
    background-color: rgb(50,55,68);
  }
  .iconmargin {
    margin-right: 10px;
  }
  .el-menu {
    border-right: 0;
  }
  .toggle-box {
    line-height: 24px;
    text-align: center;
    color: #fff;
    background-color: #4A5064;
    cursor: pointer;
  }
</style>